package cn.jeeweb.modules.monitor.controller;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.ff.common.util.meta.ReflectionUtil;
import com.ff.common.util.validate.ValidatorUtil;

import cn.jeeweb.core.common.controller.BaseCRUDController;
import cn.jeeweb.core.common.entity.AbstractEntity;
import cn.jeeweb.core.query.data.Queryable;
import cn.jeeweb.core.query.wrapper.EntityWrapper;
import cn.jeeweb.modules.sys.entity.Organization;
import cn.jeeweb.modules.sys.entity.User;
import cn.jeeweb.modules.sys.service.IOrganizationService;
import cn.jeeweb.modules.sys.utils.UserUtils;

public class McOrgBaseController<Entity extends AbstractEntity<ID>, ID extends Serializable> extends BaseCRUDController<Entity,ID>
{

	@Autowired
	IOrganizationService orgService;
	
	@Override
	public void preAjaxList(Queryable queryable,EntityWrapper<Entity> entityWrapper, HttpServletRequest request, HttpServletResponse response) {

		super.preAjaxList(queryable, entityWrapper, request, response);
		User user = UserUtils.getUser();
	 
		String val = (String) queryable.getValue("monitorId");
		if(ValidatorUtil.isEmpty(val))
		{
			entityWrapper.eq("monitorId", user.getOrganizationIds());
 		}
	}
	
	@Override
	public void preSave(Entity entity, HttpServletRequest request, HttpServletResponse response) {
		 
	    ReflectionUtil.setObjectField(entity, "monitorId", UserUtils.getUser().getOrganizationIds());
	    Organization org = orgService.selectById(UserUtils.getUser().getOrganizationIds());
	    
	    ReflectionUtil.setObjectField(entity, "monitorId", UserUtils.getUser().getOrganizationIds());
	    if(null != org)
	    {
	    	ReflectionUtil.setObjectField(entity, "monitorName", org.getName());
	    }

	}
}
